package com.jiankang.controller;

import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.jiankang.entity.Healthknowledge;
import com.jiankang.entity.Homepictures;
import com.jiankang.entity.Largeclass;
import com.jiankang.entity.User;
import com.jiankang.service.HealthknowledgeService;
import com.jiankang.service.HomepicturesService;
import com.jiankang.service.KnowledgeService;
import com.jiankang.service.LargeclassService;
import com.jiankang.service.UserService;
import com.jiankang.service.UsercommentService;
import com.jiankang.util.Data;
import com.jiankang.util.Page;
import com.jiankang.util.Pager;
import com.jiankang.util.Pagination;
@Controller
@RequestMapping("/homepictures")
public class HomepicturesController {
	@Resource
	private HomepicturesService homepicturesService;
	@Resource
	private LargeclassService largeclassService;
	@Resource
	private UsercommentService usercommentService;
	@Resource
	private UserService  userService ;
	
	@RequestMapping("/backstagesickness")  
	public String backstagesickness(HttpServletRequest request,Model model){  
		List<Largeclass> larg=largeclassService.largeclasave();
		int pageSize=18;
		Integer currentPage=0;
		if(request.getParameter("curPage")!=null)
		{
			currentPage=Integer.valueOf(request.getParameter("curPage"));
		}
		Integer startRow=(currentPage-1<0?0:currentPage-1)*Integer.valueOf(pageSize); 
		Map<String,Object> paramsMap=new HashMap<String,Object>();
		int totalCount=this.homepicturesService.selectBookListCount(paramsMap);  //总记录数
		paramsMap.put("startrow", startRow);
		paramsMap.put("pagesize", pageSize);
		Pagination pageination=new Pagination(currentPage.toString(),totalCount, pageSize, null);
		List<Homepictures> list = this.homepicturesService.selectBookList(paramsMap);
		pageination.setList(list);
		model.addAttribute("listhome", list);
		model.addAttribute("listlarg", larg);
		model.addAttribute("pageination", pageination);
		return "imglist";
	} 
	//删除
	@RequestMapping(value = "/deletehome/{id}")
	public String deletehome(@PathVariable("id") int id, Model model,HttpServletRequest request,User user, Pager pager) {
		homepicturesService.deletehome(id);
		return this.backstagesickness(request,model);
	}
	//1代表通过，0代表未通过
	@RequestMapping(value = "/updatecancelaudit")
	public String updatecancelaudit(Homepictures homepictures,HttpServletRequest request,Model model)
	{
		Homepictures j2=homepicturesService.formationdetails(homepictures.getId());

		j2.setRid(homepictures.getRid());
		homepicturesService.updatecancelaudit(j2);

		return this.backstagesickness(request,model);
	}



	//添加
	@RequestMapping(value = "/insertboudoir")
	public String insertboudoir(Model model) {
		List<Largeclass> larg=largeclassService.largeclasave();
		model.addAttribute("listlarg", larg);
		return "form";
	}


	//编辑
	@RequestMapping(value = "/editupdate/{id}")
	public String editupdate(@PathVariable("id") int id,Model model,Homepictures homepictures) {
		homepictures=homepicturesService.formationdetails(id);
		List<Largeclass> larg=largeclassService.largeclasave();
		model.addAttribute("listlarg", larg);
		model.addAttribute("homepictures", homepictures);
		return "admin/edit";
	}
	@RequestMapping(value = "/updatemodification")
	public String updatemodification(MultipartFile file, HttpServletRequest request, Model model,Homepictures homepictures) {
		if(file==null){
			System.out.println("sdfhjdgsfhjgdshfgsdhj");
			homepicturesService.updatemodification(homepictures);
			return this.backstagesickness(request, model);
		}else{
			String path = request.getSession().getServletContext().getRealPath("upload");  
			//重命名文件
			String fileName = new Date().getTime()+".jpg";  
			File targetFile = new File(path, fileName);  
			if(!targetFile.exists()){  
				targetFile.mkdirs();  
			} 
			//保存  
			try {  
				file.transferTo(targetFile);  
			} catch (Exception e) {  
				e.printStackTrace();  
			}  
			model.addAttribute("fileUrl", Data.URL.base+"/upload/"+fileName);  
			homepictures.setImages("upload/"+fileName);
			homepicturesService.updatemodification(homepictures);
			return this.backstagesickness(request, model);
		}
	}

	@RequestMapping(value = "/fileUpload")  
	public String fileUpload(@RequestParam(value = "file", required = false) MultipartFile file, HttpServletRequest request, Model model,Homepictures homepictures) {  
		String path = request.getSession().getServletContext().getRealPath("upload");  
		//重命名文件
		String fileName = new Date().getTime()+".jpg";  
		File targetFile = new File(path, fileName);  
		if(!targetFile.exists()){  
			targetFile.mkdirs();  
		} 
		//保存  
		try {  
			file.transferTo(targetFile);  
		} catch (Exception e) {  
			e.printStackTrace();  
		}  
		model.addAttribute("fileUrl", Data.URL.base+"/upload/"+fileName);  
		homepictures.setImages("upload/"+fileName);
		homepictures.setRid(0);
		homepictures.setTime(new Date());
		homepicturesService.savehome(homepictures);
		return this.backstagesickness(request, model);
	}  
	
	//健康常识
	@Resource
	private HealthknowledgeService healthknowledgeService;
	@Resource
	private KnowledgeService knowledgeService;
	@RequestMapping("/showhealthknowledge")
	public String showhealthknowledge(Model model, HttpServletRequest req)throws Exception {
		String pageNow = req.getParameter("pageNow");
		Page page = null;
		List<Healthknowledge> healthknowledge=null;
		int totalCount = (int) healthknowledgeService.getAllProductCount();
		if (pageNow != null) {
			page = new Page(totalCount, Integer.parseInt(pageNow));
			healthknowledge=healthknowledgeService.getHealthFile(page);
		}else{
			page = new Page(totalCount, 1);
			healthknowledge=healthknowledgeService.getHealthFile(page);
		}
		model.addAttribute("page", page);
		model.addAttribute("healthknowledge", healthknowledge);
		return "admin/showhealthknowledge";
	}

	//编辑
	@RequestMapping(value = "/modifyhealthc/{id}")
	public String modifyhealthc(@PathVariable("id") int id,Model model,Healthknowledge healthknowledge) {
		healthknowledge=healthknowledgeService.commonsensedetails(id);
		model.addAttribute("healthknowledge", healthknowledge);
		return "admin/jsp/modifyhealthc";
	}
}	